package com.fwj.portal.service.business.activity;

import com.alibaba.fastjson.JSONObject;
import com.fwj.core.common.common.CommonUtils;
import com.fwj.core.common.constant.DataConstant;
import com.fwj.core.common.model.PageRequest;
import com.fwj.core.common.model.PageResponse;
import com.fwj.core.common.model.ResponseDTO;
import com.fwj.portal.api.business.activity.BusinessActivityService;
import com.fwj.portal.dao.business.activity.BusinessActivityMapper;
import com.fwj.portal.entity.activity.BusinessActivity;
import com.fwj.portal.entity.product.BusinessProductInfo;
import com.fwj.portal.model.business.activity.BusinessActivityVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: fwj
 * @Data: 2022/5/7 21:52
 * @Version 1.0
 * @Description:
 */

@Service
public class BusinessActivityServiceImpl implements BusinessActivityService {


    // 导入日志
    private static final Logger LOGGER = LoggerFactory.getLogger(BusinessActivityServiceImpl.class);

    @Autowired
    private BusinessActivityMapper businessActivityMapper;
    /**
     * Description: TODO 保存活动信息 => (发布、修改)
     * Author: fwj
     */
    @Override
    public ResponseDTO saveActivity(BusinessActivity businessActivity) {
        ResponseDTO responseDTO = new ResponseDTO();
        try {
            // 执行更新
            if (CommonUtils.isNotEmpty(businessActivity.getActivityId())){
                updateBusinessActivity(businessActivity);
            }
            // 执行插入
            if (CommonUtils.isEmpty(businessActivity.getActivityId())){
                insertBusinessActivity(businessActivity);
            }
            return responseDTO.success();
        } catch (Exception e) {
            LOGGER.error(e.toString());
            return responseDTO.fail();
        }
    }

    private void insertBusinessActivity(BusinessActivity businessActivity) throws Exception{
        BusinessActivity execBusinessActivity = buildBusinessActivity(businessActivity);
        businessActivityMapper.insertSelective(execBusinessActivity);
    }

    private BusinessActivity buildBusinessActivity(BusinessActivity businessActivity) throws Exception{
        BusinessActivity ret = JSONObject.parseObject(JSONObject.toJSONString(businessActivity),BusinessActivity.class);
        ret.setActivityId(CommonUtils.generateUniqueNumber());
        LocalDateTime now = LocalDateTime.now();
        ret.setReleaseTime(now);
        ret.setUpdateTime(now);
        ret.setStatus(DataConstant.NORMAL);
        return ret;
    }

    private void updateBusinessActivity(BusinessActivity businessActivity) throws Exception{
        BusinessActivity execBusinessActivity = new BusinessActivity();
        execBusinessActivity.setCreator(null);
        execBusinessActivity.setUpdateTime(LocalDateTime.now());
        Example example = Example.builder(BusinessProductInfo.class).build();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("activityId",execBusinessActivity.getActivityId());
        criteria.andEqualTo("status",DataConstant.NORMAL);
        businessActivityMapper.updateByExampleSelective(execBusinessActivity,example);
    }

    /**
     * Description: TODO 删除活动信息
     * Author: fwj
     */
    @Override
    public ResponseDTO deleteActivity() {
        return null;
    }

    /**
     * Description: TODO 查询产品列表信息
     * Author: fwj
     */
    @Override
    public ResponseDTO queryActivityList(PageRequest pageRequest) {
        ResponseDTO responseDTO = new ResponseDTO();
        try {
            PageHelper.startPage(pageRequest.getPage(), pageRequest.getPageSize());
            Example example = new Example(BusinessActivity.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andLessThan("releaseTime",LocalDateTime.now());
            criteria.andEqualTo("status",DataConstant.NORMAL);
            PageInfo<BusinessActivity> pageInfo = new PageInfo<>(businessActivityMapper.selectByExample(example));
            List<BusinessActivityVo> businessActivityVoList = JSONObject.parseArray(JSONObject.toJSONString(pageInfo.getList()),BusinessActivityVo.class);
            PageResponse pageResponse = new PageResponse();
            pageResponse.setPageTotal(pageInfo.getTotal());
            pageResponse.setResponseBody(businessActivityVoList);
            return responseDTO.success().setData(pageResponse);
        } catch (Exception e) {
            LOGGER.error(e.toString());
            return responseDTO.fail();
        }
    }

    /**
     * Description: TODO 获取产品详情信息
     * Author: fwj
     */
    @Override
    public ResponseDTO getActivityDetails() {
        return null;
    }
}
